package avicit.bdp.dds.dao.entity;

import avicit.bdp.common.base.BaseDTO;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.platform6.core.annotation.log.FieldRemark;
import avicit.platform6.core.annotation.log.Id;
import avicit.platform6.core.annotation.log.LogField;
import avicit.platform6.core.properties.PlatformConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @author DIGITAL-MAYANJ
 * 流程配置-资源配置
 */
@ApiModel("流程配置-资源配置")
@Table(name = "bdp_dds_process_resource_settings")
public class ProcessResourceSettings extends BaseDTO {

    public static final int TYPE_SPARK = 0;
    public static final int TYPE_FLINK = 1;

    /**
     * id
     */
    @Id
    @LogField
    @ApiModelProperty(value = "id", name = "id")
    @FieldRemark(column = "id", field = "id", name = "主键")
    @javax.persistence.Id
    @Column(name = "id")
    private String id;

    /**
     * 流程定义ID
     */
    @LogField
    @ApiModelProperty(value = "流程定义ID", name = "processDefinitionId")
    @FieldRemark(column = "process_definition_id", field = "processDefinitionId", name = "流程定义ID")
    @Column(name = "process_definition_id")
    private String processDefinitionId;

    /**
     * 计算资源ID
     */
    @LogField
    @ApiModelProperty(value = "计算资源ID", name = "engineResourceId")
    @FieldRemark(column = "engine_resource_id", field = "engineResourceId", name = "计算资源ID")
    @Column(name = "engine_resource_id")
    private String engineResourceId;


    @LogField
    @ApiModelProperty(value = "类型：0--spark；1--Flink", name = "type")
    @FieldRemark(column = "type", field = "type", name = "类型：0--spark；1--Flink")
    @Column(name = "type")
    private Integer type;


    @LogField
    @ApiModelProperty(value = "部署方式", name = "deployMode")
    @FieldRemark(column = "deploy_mode", field = "deployMode", name = "部署方式")
    @Column(name = "deploy_mode")
    private String deployMode;


    @LogField
    @ApiModelProperty(value = "部署参数", name = "deployParams")
    @FieldRemark(column = "deploy_params", field = "deployParams", name = "部署参数")
    @Column(name = "deploy_params")
    private String deployParams;


    @LogField
    @ApiModelProperty(value = "排序", name = "orderBy")
    @FieldRemark(column = "order_by", field = "orderBy", name = "排序")
    @Column(name = "order_by")
    private int orderBy;


    @Transient
    @ApiModelProperty("spark")
    private SparkConfig sparkConfig;

    @Transient
    @ApiModelProperty("flink")
    private FlinkConfig flinkConfig;

    public String getProcessDefinitionId() {
        return processDefinitionId;
    }

    public void setProcessDefinitionId(String processDefinitionId) {
        this.processDefinitionId = processDefinitionId;
    }

    public SparkConfig getSparkConfig() {
        if (type == TYPE_SPARK && StringUtils.isNotBlank(deployParams)) {
            try {
                SparkConfig sparkConfig = JSONUtils.parseObject(deployParams, SparkConfig.class);
//                if (sparkConfig != null) {
//                    sparkConfig.setDeployMode(deployMode);
//                    sparkConfig.setClusterId(engineResourceId);
//                }
                return sparkConfig;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sparkConfig;
    }

    public void setSparkConfig(SparkConfig sparkConfig) {
        this.sparkConfig = sparkConfig;
    }

    public FlinkConfig getFlinkConfig() {
        if (type == TYPE_FLINK && StringUtils.isNotBlank(deployParams)) {
            try {
                FlinkConfig flinkConfig = JSONUtils.parseObject(deployParams, FlinkConfig.class);
                if (flinkConfig != null) {
                    flinkConfig.setDeployMode(deployMode);
                    flinkConfig.setClusterId(engineResourceId);
                }
                return flinkConfig;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return flinkConfig;
    }

    public void setFlinkConfig(FlinkConfig flinkConfig) {
        this.flinkConfig = flinkConfig;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEngineResourceId() {
        return engineResourceId;
    }

    public void setEngineResourceId(String engineResourceId) {
        this.engineResourceId = engineResourceId;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getDeployMode() {
        return deployMode;
    }

    public void setDeployMode(String deployMode) {
        this.deployMode = deployMode;
    }

    public String getDeployParams() {
        return deployParams;
    }

    public void setDeployParams(String deployParams) {
        this.deployParams = deployParams;
    }

    public int getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(int orderBy) {
        this.orderBy = orderBy;
    }

    @Override
    public String getLogFormName() {
        if (avicit.platform6.commons.utils.StringUtils.isEmpty(super.logFormName)) {
            return "数据开发模块";
        } else {
            return super.logFormName;
        }
    }

    @Override
    public String getLogTitle() {
        if (avicit.platform6.commons.utils.StringUtils.isEmpty(super.logTitle)) {
            return "流程配置-资源配置";
        } else {
            return super.logTitle;
        }
    }

    @Override
    public PlatformConstant.LogType getLogType() {
        if (super.logType == null) {
            return PlatformConstant.LogType.module_operate;
        } else {
            return super.logType;
        }
    }

}
